#include<iostream>
#include<sstream>
#include<string>
#include<memory>
#include<jsoncpp/json/json.h>

void serialize()
{
	const char* name="小明";
	int age=19;
	float score[]={77.5,88,99.5};

	Json::Value val;
	val["姓名"]=name;
	val["年龄"]=19;
	val["成绩"].append(score[0]);
	val["成绩"].append(score[1]);
	val["成绩"].append(score[2]);

	Json::StreamWriterBuilder swb;
	std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());

	std::stringstream ss;
	int ret=sw->write(val,&ss);
	if(ret!=0){
		std::cout<<"write faiked!\n";
		return;
	}
	std::cout<<ss.str()<<std::endl;
}
void unserialize(const std::string &str){
	Json::Value val;
	Json::CharReaderBuilder crb;
	std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
	
	std::string err;
	bool ret=cr->parse(str.c_str(),str.c_str()+str.size(),&val,&err);
	if(ret==false){
	std::cout<<"parse failed!\n";
	return ;
	}
	std::cout<<val["姓名"].asString()<<std::endl;
	std::cout<<val["年龄"].asInt()<<std::endl;
	int sz=val["成绩"].size();
	for(int i=0;i<sz;i++){
		std::cout<<val["成绩"][i]<<std::endl;
	}
}
int main(){
	std::string str=R"({"姓名":"小黑","年龄":18,"成绩":[66,77,88.5]})";
	unserialize(str);
	return 0;
}
